use CreativeSystems;
GO

ALTER PROC pr_GetTransactions
(
	@StartDateRange DATETIME,
	@EndDateRange DATETIME,
	@ClientName VARCHAR(MAX),
	@PaymentType VARCHAR(MAX)	
)
AS

	SELECT [DateTime] "Date", c.FirstName + ' ' + c.LastName "Client", PaymentType "Type", Amount
	FROM Payment p
	INNER JOIN Invoice i ON i.InvoiceID = p.InvoiceID
	INNER JOIN [Event] e ON e.EventID = i.EventID
	INNER JOIN Client c ON c.ClientID = e.ClientID
	WHERE [DateTime] >= @StartDateRange
	AND [DateTime] <= @EndDateRange
	AND c.FirstName + ' ' + c.LastName = COALESCE(@ClientName, c.FirstName + ' ' + c.LastName)
	AND PaymentType = COALESCE(@PaymentType, PaymentType)
	Order By [DateTime], c.FirstName + ' ' + c.LastName;